package com.iaiai.imail.dao.impl;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.iaiai.imail.dao.EmailDao;
import com.iaiai.imail.entity.Email;
import com.iaiai.imail.helper.DatabaseOpenHelper;

/**
 * 
 * <p>
 * Title: EmailDaoImpl.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2012-8-20 下午3:03:17
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class EmailDaoImpl implements EmailDao {

	public List<Email> queryAll() {
		SQLiteDatabase sqlite = DatabaseOpenHelper.getInstance().getReadableDatabase();
		Cursor cursor = sqlite.query("EMAIL", null, null, null, null, null, null);
		List<Email> list = new ArrayList<Email>();
		
		while(cursor.moveToNext()){  
            list.add(getEntity(cursor));
        }
		
		cursor.close();
		sqlite.close();
		return list;
	}
	
	public Email queryEmail(String account){
		Email email = null;
		
		SQLiteDatabase sqlite = DatabaseOpenHelper.getInstance().getReadableDatabase();
		Cursor cursor = sqlite.rawQuery("select * from EMAIL where ACCOUNT=?", new String[]{account});
		if(cursor.moveToNext()){
			email = getEntity(cursor);
		}
		
		cursor.close();
		sqlite.close();
		return email;
	}
	
	public void createEmail(Email email){
		SQLiteDatabase sqlite = DatabaseOpenHelper.getInstance().getWritableDatabase();
		sqlite.execSQL(
				"insert into EMAIL(" +
						"ACCOUNT" +
						",USERNAME" +
						",PASSWORD" +
						",CREATEDATE" +
						",GETPORT" +
						",SENDPORT" +
						",GETSERVICE" +
						",SENDSERVICE" +
						",UVALIDATE" +
						",RCOUNT" +
						") values(?,?,?,?,?,?,?,?,?,?)", new Object[]{
							email.getAccount(),
							email.getUserName(),
							email.getPassword(),
							email.getCreateDate(),
							email.getServicePort(),
							email.getSendPort(),
							email.getServiceAddress(),
							email.getSendService(),
							email.getUvalidate(),
							0
						});
		sqlite.close();
	}
	
	private Email getEntity(final Cursor cursor){
		Email email = new Email();
        email.setId(cursor.getInt(cursor.getColumnIndex("ID")));
        email.setAccount(cursor.getString(cursor.getColumnIndex("ACCOUNT")));	//帐号
        email.setUserName(cursor.getString(cursor.getColumnIndex("USERNAME")));	//用户名
        email.setPassword(cursor.getString(cursor.getColumnIndex("PASSWORD")));	//密码
        email.setCreateDate(cursor.getString(cursor.getColumnIndex("CREATEDATE")));	//创建日期
        email.setServicePort(cursor.getInt(cursor.getColumnIndex("GETPORT")));	//获得邮件端口[默认是pop3的110端口]
        email.setSendPort(cursor.getInt(cursor.getColumnIndex("SENDPORT")));	//发送邮件端口[默认是smtp的25端口]
        email.setServiceAddress(cursor.getString(cursor.getColumnIndex("GETSERVICE")));	//接收邮件服务器[如：pop.qq.com]
        email.setSendService(cursor.getString(cursor.getColumnIndex("SENDSERVICE")));	//发送邮件服务器[如：smtp.qq.com]
        email.setUvalidate(cursor.getInt(cursor.getColumnIndex("UVALIDATE")));	//发送邮件身份验证
        email.setRcount(cursor.getInt(cursor.getColumnIndex("RCOUNT")));	//接收次数
        return email;
	}
	
	public void updateEmail(Email email){
		SQLiteDatabase sqlite = DatabaseOpenHelper.getInstance().getWritableDatabase();
		sqlite.execSQL(
				"update EMAIL set ACCOUNT=?,USERNAME=?,PASSWORD=?,CREATEDATE=?,GETPORT=?,SENDPORT=?,GETSERVICE=?,SENDSERVICE=?,UVALIDATE=?,RCOUNT=? where ID=?", new Object[]{
							email.getAccount(),
							email.getUserName(),
							email.getPassword(),
							email.getCreateDate(),
							email.getServicePort(),
							email.getSendPort(),
							email.getServiceAddress(),
							email.getSendService(),
							email.getUvalidate(),
							email.getRcount(),
							email.getId()
						});
		sqlite.close();
	}
	
}
